import { useActProcessNodeStoreWithout } from '/@/store/modules/cn/core/act/actProcessNodeStore'
import { useActProcessNodeDataStoreWithout } from '/@/store/modules/cn/core/act/actProcessNodeDataStore'

export default class CustomContextPad {
  constructor(config, contextPad, create, elementFactory, injector, translate) {
    this.create = create;
    this.elementFactory = elementFactory;
    this.translate = translate;

    if (config.autoPlace !== false) {
      this.autoPlace = injector.get('autoPlace', false);
    }

    contextPad.registerProvider(this);
  }

  getContextPadEntries(element) {
    const {
      autoPlace,
      create,
      elementFactory,
      translate
    } = this;

    function appendUserTask(event, element) {
      if (autoPlace) {
        const shape = elementFactory.createShape({ type: 'bpmn:UserTask' });
  
        autoPlace.append(element, shape);
      } else {
        appendUserTaskStart(event, element);
      }
    }

    function appendUserTaskStart(event) {
      const shape = elementFactory.createShape({ type: 'bpmn:UserTask' });
  
      create.start(event, shape, element);
    }

    function appendUserTaskInfo(event, element) {
      if ('bpmn:UserTask' === element.type) {
        const actProcessNodeStore = useActProcessNodeStoreWithout()
        const actProcessNodeDataStore = useActProcessNodeDataStoreWithout()
        actProcessNodeDataStore.setAssignee(element.businessObject.assignee)
        actProcessNodeDataStore.setCandidateUsers(element.businessObject.candidateUsers)
        actProcessNodeDataStore.setCandidateGroups(element.businessObject.candidateGroups)
        actProcessNodeStore.setActProcessUserTaskNodeId(element.id)
      }
    }

    let rEntries = {}

    let userTask = {
      'append.user-task': {
        group: 'model',
        className: 'bpmn-icon-user-task',
        title: translate('Append UserTask'),
        action: {
          click: appendUserTask,
          dragstart: appendUserTaskStart
        }
      }
    }

    let userTaskInfo = {
      'append.user-task-info': {
        group: 'model',
        className: 'bpmn-icon-user',
        title: translate('关联员工或岗位'),
        action: {
          click: appendUserTaskInfo
        }
      }
    }

    if ('bpmn:UserTask' === element.type || 'bpmn:StartEvent' === element.type || 'bpmn:Task' === element.type) {
      rEntries = { ...rEntries, ...userTask }
    }

    if ('bpmn:UserTask' === element.type) {
      rEntries = { ...rEntries, ...userTaskInfo }
    }

    return rEntries;
  }
}

CustomContextPad.$inject = [
  'config',
  'contextPad',
  'create',
  'elementFactory',
  'injector',
  'translate'
];